home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 0838 / sample / main.c < prev    next >
C/C++ Source or Header  |  1994-10-17  |  16KB  |  496 lines

  1. /* **************************************************************
  2.  *
  3.  *
  4.  *      C W   :   B o u c l e    P r i n c i p a l e
  5.  * 
  6.  *
  7.  * **************************************************************** */
  8.  
  9. #define  STRICT
  10. #include <windows.h>
  11. #include <windowsx.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16.  
  17. /* messages particuliers */
  18. #define  CW_BEGIN    (WM_USER + 41)
  19. #define  CW_LOGGED   (WM_USER + 48)
  20. #define  CW_END      (WM_USER + 52)
  21. #define  CW_DIR      (WM_USER + 51)
  22. #define  CW_FIN      (WM_USER + 65)
  23. #define  CW_VERBOSE  (WM_USER+112)
  24.  
  25. #include "wftp.h"
  26.  
  27. /* Quelques variables globales */
  28.  
  29. int nHorzSiz, nVertSiz;
  30. int  nVertPos, nHorzPos;           // scroll pos for debug window
  31. int  nHorzMax, nCurMsg;
  32. int  nMaxMsg;
  33. static int nHorzTab = 20;
  34.  
  35.  
  36. char            szApplication[20]; /* name of application      */
  37. HINSTANCE       hInst;             /* hInstance of application */
  38. HWND            hwnd;              /* hWnd of main window      */
  39. static unsigned Timer;             /* frΘquence de reveil      */
  40. static unsigned TimeRest;          /* Gestion Timer > minute   */
  41. BOOL            FirstEmission;
  42.  
  43. // function prototypes
  44. int PASCAL FAR  DLL_Work (HWND, int);
  45. int PASCAL FAR  DLL_FT   (HWND, BOOL, int);
  46. int PASCAL FAR  DLL_End (void);
  47. int PASCAL FAR  DLL_Dir (HWND hParent, BOOL First, UINT Msg);
  48.  
  49. void SetXmitBytes (LONG lBytes);
  50. void DeleteXferWindow (void);
  51. int nCwRegisterClasses(void);
  52. int nCwUnregisterClasses(void);
  53.  
  54.  
  55. int PASCAL FAR InitWindow (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  56.                            LPSTR lpszCmdLine, int cmdShow);
  57. int Ecris(const char *szFormat,...);
  58. void DoPrintf (char * szFormat, ...);
  59. void DoPaint (HWND hwnd);
  60. void DoAddLine (LPSTR szLine);                           
  61. void ReleaseDisplayMem(void);
  62.  
  63.  
  64.  
  65. #define  WM_FTPDIR  (WM_USER+2)
  66. #define  SHORT_DIR  FALSE
  67.  
  68. struct LL_File
  69. {
  70.   LPSTR          *szFile;
  71.   struct LL_File *Next;
  72. } *pFirstFile=NULL;
  73.  
  74.  
  75.  
  76.  
  77.  
  78. //*******************************************************************
  79. LRESULT CALLBACK   MainWndProc (HWND hWnd, UINT message,
  80.                                 WPARAM wParam, LPARAM lParam)
  81. {
  82. static      IsMinimized = FALSE;
  83. static      nClose=0;
  84. static      bFileTransfered=FALSE;
  85. static      bAborted=FALSE;
  86. static struct S_ProcData *pProcData;
  87. static struct LL_File *p, *pTmp;
  88. static char szBuf[1024];
  89. LPSTR p1, p2;
  90.  
  91.     switch (message)
  92.     {
  93.        case WM_CREATE :
  94.           /* Gestion de la fenΩtre debug */
  95.           SetScrollPos (hWnd, SB_HORZ, 0, TRUE);
  96.           SetScrollPos (hWnd, SB_VERT, 0, TRUE);
  97.           SetScrollRange (hWnd, SB_HORZ, 0, 1, TRUE);
  98.           SetScrollRange (hWnd, SB_VERT, 0, 1, TRUE);
  99.           nVertPos = nHorzPos = nHorzMax = nCurMsg = 0;
  100.           nMaxMsg = 80;
  101.    
  102.           nCwRegisterClasses();
  103.           /* On attend que la fenetre apparaisse et on debute les inits */
  104.           PostMessage (hWnd, CW_BEGIN, 0, 0);
  105.           break;
  106.  
  107.        case WM_SIZE :
  108.           IsMinimized = wParam==SIZE_MINIMIZED;
  109.           break;    
  110.  
  111.        case WM_VSCROLL:
  112.           switch(wParam)
  113.            {
  114.             case SB_LINEDOWN     : if (nVertPos<(nCurMsg-(nVertSiz>>1))) nVertPos++;  break;
  115.             case SB_LINEUP       : if (nVertPos>0) --nVertPos; break;
  116.             case SB_THUMBPOSITION: nVertPos = min ((WORD) (nCurMsg-(nVertSiz>>1)), LOWORD (lParam)); break;
  117.             case SB_PAGEUP       : nVertPos = (nVertPos>10) ? (nVertPos-10) : 0; break;
  118.             case SB_PAGEDOWN     : nVertPos = (nVertPos<(nCurMsg-nVertSiz)) ? (nVertPos+nVertSiz) : nCurMsg-(nVertSiz>>1); break;
  119.             default              : return 0L;
  120.            } /* switch wParam */
  121.           SetScrollPos (hWnd, SB_VERT, nVertPos, TRUE);
  122.           InvalidateRect (hWnd, NULL, TRUE); 
  123.           return 0L;
  124.  
  125.        case WM_HSCROLL:
  126.           FtpAbort ();
  127.           switch (wParam)
  128.            {
  129.             case SB_LINEDOWN     : nHorzPos = (nHorzPos<(nHorzMax-nHorzTab)) ? (nHorzPos+nHorzTab) : nHorzMax; break;
  130.             case SB_LINEUP       : nHorzPos = (nHorzPos>nHorzTab) ? (nHorzPos-nHorzTab) : 0; break;
  131.             case SB_THUMBPOSITION: nHorzPos = min ((WORD) nHorzMax, LOWORD (lParam)); break;
  132.             case SB_PAGEUP       : nHorzPos = (nHorzPos>nHorzSiz) ? (nHorzPos-nHorzSiz) : 0; break;
  133.             case SB_PAGEDOWN     : nHorzPos = (nHorzPos<(nHorzMax-nHorzSiz)) ? (nHorzPos+nHorzSiz) : nHorzMax; break;
  134.             default              : return 0L;
  135.            }
  136.           SetScrollPos (hWnd, SB_HORZ, nHorzPos, TRUE);
  137.           InvalidateRect (hWnd, NULL, TRUE); 
  138.           return 0L;
  139.  
  140.       case WM_PAINT :
  141.           DoPaint (hWnd);
  142.           break;
  143.           
  144.    /* ---------------------------------------------------------- */
  145.    /*              Code  spΘcifique  aux collecteurs             */
  146.    /* ---------------------------------------------------------- */
  147.  
  148.       case WM_QUERYENDSESSION :
  149.           FtpLocalClose ();
  150.           DeleteXferWindow ();
  151.           break;
  152.  
  153.       case CW_VERBOSE :
  154.           DoAddLine ((LPSTR) lParam);
  155.           break;
  156.  
  157.       case  CW_BEGIN :
  158.             /* Get UINIFile Name */
  159.           Ecris ("--- CW ---");  
  160.           if (FirstEmission) unlink ("traces");
  161.           DLL_Work (hWnd, CW_LOGGED);
  162.           break;
  163.  
  164.  
  165.       case CW_LOGGED :
  166.           Ecris ("-- LOGIN  -> %d, %d", wParam, (int) lParam);  
  167.           bFileTransfered = FALSE;
  168.           if (lParam == FTPERR_OK)
  169.             {
  170.               memset (szBuf, 0, sizeof szBuf);
  171. //              lParam = FtpHelp (NULL, szBuf, sizeof szBuf);
  172.               p1=szBuf;
  173.               for (;0;)
  174.                {
  175.                  p2 = strchr (p1, '\r');
  176.                  if (p2==NULL) break;
  177.                  *p2 = 0;
  178.                  Ecris ( p1 );
  179.                  p1 = p2+2;
  180.                 }
  181.               Ecris (p1);
  182.              DLL_Dir (hWnd, FirstEmission, WM_FTPDIR);
  183. //           DLL_FT (hWnd, FirstEmission, CW_END);
  184.             }
  185.           if (lParam==FTPERR_UNEXPECTEDANSWER)
  186.              Ecris (FtpDataPtr()->ftp.szInBuf);   
  187.           break;
  188.  
  189.  
  190.       case WM_FTPDIR :
  191.        if (wParam)
  192.           {
  193.              Ecris ("Reception %d, %d", wParam, lParam);
  194.              for (p=pFirstFile ; p!=NULL ; p=pTmp)
  195.                {
  196.                  Ecris (p->szFile);
  197.                  free (p->szFile);
  198.                  pTmp = p->Next;
  199.                  free (p);
  200.                }
  201.              pFirstFile=NULL;
  202.              Ecris ("---- Transfert Fini ----- %d, %d", wParam, (WORD) lParam);
  203.              Ecris ("Reponse : [%s]", FtpDataPtr ()->ftp.szInBuf);
  204.              DLL_FT (hWnd, FirstEmission, CW_END);
  205.           }
  206.        else
  207.            {
  208.              if (pFirstFile==NULL) p = pFirstFile=calloc (sizeof *pFirstFile, 1);
  209.              else                  p = p->Next   =calloc (sizeof *pFirstFile, 1);
  210.              p->szFile = strdup ((LPSTR) lParam);
  211.            }
  212.        break;          
  213.  
  214.       case CW_END :
  215.          if (wParam)
  216.            {
  217.             DeleteXferWindow ();
  218.             bFileTransfered = TRUE;
  219.             Ecris ("FTF %d, %ld", wParam, lParam);
  220.             pProcData = FtpDataPtr ();
  221.             if (pProcData!=NULL)  
  222.               {
  223.                  Ecris ("Pointeur %p", pProcData);
  224.                  Ecris ("Buffer |%s|", pProcData->ftp.szInBuf);
  225.               }
  226.            } /* wPram */
  227.           else
  228.            {
  229.              SetXmitBytes (lParam);
  230.            }
  231.           break;
  232.  
  233.       case  WM_TIMER :
  234.             break;
  235.             
  236.       case CW_FIN :
  237.           Ecris ("CW FIN");
  238.           break;
  239.  
  240.       case WM_CLOSE :
  241.           Ecris ("Close %d", ++nClose);  
  242.           if (! bFileTransfered) FtpAbort (), bAborted=TRUE;
  243.           else if (nClose==2)
  244.              {
  245.                 if (MessageBox (hWnd,
  246.                                "Refaire un transfert ?", 
  247.                                "Test FTP",
  248.                                MB_YESNO) == IDYES)
  249.                   { PostMessage (hWnd, CW_LOGGED, 0, 0);
  250.                     MessageBeep (65535u); 
  251.                     nClose=0;
  252.                     return 0; } 
  253.                 Ecris ("---> Closing connection ");
  254.                 DLL_End ();
  255.                 ReleaseDisplayMem();
  256.                 /* Tell windows to destroy our window.*/
  257.                 nCwUnregisterClasses();
  258.                 PostQuitMessage(0);
  259.                 DestroyWindow (hWnd);
  260.              }
  261.  
  262.           if (hWnd==hwnd  && nClose>=3)
  263.            { 
  264.              /* si l'utilsateur n'attend pas l'exΘcution du ABORT */
  265.              FtpLocalClose ();
  266.              /* This is the end if we were closed by a DestroyWindow call.*/
  267.              ReleaseDisplayMem();
  268.              /* Tell windows to destroy our window.*/
  269.              nCwUnregisterClasses();
  270.              PostQuitMessage(0);
  271.              DestroyWindow (hWnd);
  272.            }
  273.           else
  274.             return 0;
  275.           break;
  276.  
  277.  
  278.     default:
  279.           return(DefWindowProc(hWnd, message, wParam, lParam));
  280.     }
  281. return TRUE;
  282. } /* Boucle Windows */
  283.  
  284.  
  285.  
  286.  
  287. /* ******************************************************************* */
  288. int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  289.                     LPSTR lpszCmdLine, int cmdShow)
  290. {
  291. MSG   msg;
  292.     // Go init this application, Change default Font
  293.     FirstEmission = (hPrevInstance==NULL);
  294.     
  295.     InitWindow (hInstance, hPrevInstance, lpszCmdLine, cmdShow);
  296.     /* Get and dispatch messages for this applicaton.*/
  297.     while (GetMessage(&msg, NULL, 0, 0))
  298.       {
  299.         TranslateMessage(&msg);
  300.         DispatchMessage(&msg);                  
  301.       }
  302.     UnregisterClass ("DLLCW", hInst);  
  303.     UnregisterClass ("CW", hInst);  
  304. return(msg.wParam);
  305. } /* WinMain */
  306.  
  307.  
  308. /* ******************************************************************* */
  309.  
  310. int PASCAL FAR InitWindow (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  311.                            LPSTR lpszCmdLine, int cmdShow)
  312. {
  313. WNDCLASS wndClass;
  314.  
  315.     hInst = hInstance;       /* save for use by window procs */
  316.     strcpy (szApplication, "CW");
  317.  
  318.     if (!hPrevInstance)     /* if no previous instance, this is first */
  319.       {
  320.         /* Get string from resource with application name. */
  321.         /* Define the window class for this application. */
  322.         wndClass.lpszClassName = szApplication;
  323.         wndClass.hInstance     = hInstance;
  324.         wndClass.lpfnWndProc   = MainWndProc;
  325.         wndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  326.         wndClass.hIcon         = LoadIcon(hInstance, szApplication);
  327.         wndClass.lpszMenuName  = (LPSTR) NULL;
  328.         wndClass.hbrBackground = COLOR_WINDOW+1;
  329.         // style : Redraw window if its vertcal size has been changed
  330.         wndClass.style         = CS_OWNDC | CS_VREDRAW;
  331.         wndClass.cbClsExtra    = 0;
  332.         wndClass.cbWndExtra    = 0;
  333.      // Register the class
  334.         RegisterClass(&wndClass);
  335.       }
  336.     hwnd = CreateWindow(
  337.                     szApplication,     // window class name
  338.                     szApplication,     // window title
  339.                     WS_OVERLAPPEDWINDOW | WS_HSCROLL | WM_VSCROLL | WS_VISIBLE,
  340.                     CW_USEDEFAULT,          // x - same as dialog box
  341.                     CW_USEDEFAULT,          // y
  342.                     250,                     // cx
  343.                     150,                     // cy
  344.                     NULL,                    // no parent for this window
  345.                     NULL,                    // use the class menu
  346.                     hInstance,               // who created this window
  347.                     NULL                     // no parms to pass on
  348.           );
  349.     if (hwnd==NULL)  exit(0);
  350.     UpdateWindow (hwnd);
  351.     ShowWindow (hwnd, cmdShow);
  352.  
  353. return 0;
  354. }  /* InitWindows */
  355.  
  356.  
  357.  
  358.  
  359. //*************************************************************************
  360. //*************************************************************************
  361.  
  362. /* ------------------------------------------------------- */
  363. /*          Affichage d'un texte a l'ecran                 */
  364. /* ------------------------------------------------------- */
  365. /* Attention : Pas de test de debordement                  */
  366.  
  367.  
  368. #include <stdarg.h>
  369.  
  370.  
  371.  
  372.  
  373. int Ecris(const char *szFormat,...)
  374. {
  375.    va_list vaArgs;
  376.    static char szBuf[256];
  377.  
  378.    va_start(vaArgs,szFormat);
  379.    if (vsprintf (szBuf,szFormat,vaArgs)!=EOF) DoAddLine (szBuf);
  380.    va_end(vaArgs);
  381. return 0;
  382. } /* Ecris */
  383.  
  384.  
  385. //*************************************************************************
  386. //*************************************************************************
  387. static HWND hWndViewMsg = (HWND) NULL;
  388. static LPSTR *lpMsg;
  389. static LPSTR szNoMem = "Unable to allocate memory!";
  390. static LPSTR szZoomErr = "Zoom Error";
  391. static BOOL bZoomFlag=FALSE;
  392.  
  393.  
  394.  
  395. void DoAddLine (LPSTR lpBuf)
  396. {
  397. LPSTR lp;
  398.  
  399.   // added in some error checking to try to eliminate GPFs
  400.   if (lpBuf==NULL || lpBuf[0]==0)   return;
  401.   if (lpMsg==NULL)
  402.     {
  403.       lpMsg = (LPSTR *) GlobalAllocPtr (GHND, nMaxMsg * sizeof (LPSTR));
  404.       nCurMsg = 0;
  405.       if (lpMsg==NULL) 
  406.         {
  407.           MessageBox (hwnd, szNoMem, szZoomErr, MB_OK);
  408.           return;
  409.         }
  410.     }
  411.   if (nCurMsg>=nMaxMsg)
  412.     {
  413.       GlobalFreePtr (lpMsg[0]);
  414.       memmove (lpMsg, lpMsg+1, (nMaxMsg-1)*sizeof (LPSTR));
  415.       nCurMsg = nMaxMsg-1;
  416.     }
  417.   lp = lpMsg[nCurMsg] = (LPSTR) GlobalAllocPtr (GHND, lstrlen (lpBuf) + 5);
  418.   if (lp!=NULL) 
  419.     {
  420.       HDC hDC;
  421.       SIZE siz;
  422.       
  423.       lstrcpy (lp, lpBuf);
  424.       hDC = GetDC (hwnd);
  425.       GetTextExtentPoint (hDC, lp, lstrlen (lp), &siz);
  426.       ReleaseDC (hwnd, hDC);
  427.       nHorzMax = __max (nHorzMax, siz.cx);
  428.       SetScrollRange (hwnd, SB_HORZ, 0, nHorzMax, TRUE);
  429.       SetScrollRange (hwnd, SB_VERT, 0, nCurMsg, TRUE);
  430.       InvalidateRect (hwnd, NULL, TRUE);
  431.       nCurMsg++;
  432.     }  /* lp not NULL */
  433.     else 
  434.     {
  435.       MessageBox (hwnd, szNoMem, szZoomErr, MB_OK);
  436.       return;
  437.     } /* no more memory */
  438. } /* DoAddLine */
  439.  
  440.  
  441. //*************************************************************************
  442. //*************************************************************************
  443. void ReleaseDisplayMem()
  444. {
  445. int nI;
  446.   if (lpMsg!=NULL) 
  447.   {
  448.     for (nI=0; nI<nMaxMsg; nI++) 
  449.     {
  450.       if (lpMsg[nI]!=NULL) GlobalFreePtr (lpMsg[nI]);
  451.       lpMsg[nI] = NULL;
  452.     }
  453.   }
  454.   nCurMsg = nVertPos = nHorzPos = nHorzMax = 0;
  455.   SetScrollPos (hwnd, SB_HORZ, 0, TRUE);
  456.   SetScrollPos (hwnd, SB_VERT, 0, TRUE);
  457.   SetScrollRange (hwnd, SB_HORZ, 0, 1, TRUE);
  458.   SetScrollRange (hwnd, SB_VERT, 0, 1, TRUE);
  459. } /* ReleaseDisplymemory */
  460.  
  461.  
  462. //*************************************************************************
  463. void DoPaint(HWND hWnd)
  464. {
  465. HDC         hDC;
  466. PAINTSTRUCT ps;
  467. int         nI, nRows, nLine, nPos, nHt;
  468. RECT        rRect;
  469. TEXTMETRIC  tm;
  470.  
  471.   memset (& ps, 0, sizeof ps);  
  472.   hDC = BeginPaint (hWnd, &ps);
  473.   // Included as the background is not a pure color
  474.   SetBkMode(hDC, TRANSPARENT);
  475.   GetTextMetrics (hDC,&tm);
  476.   nHt = tm.tmHeight + tm.tmExternalLeading;
  477.   GetClientRect (hWnd, &rRect);
  478.   nHorzSiz = rRect.right;
  479.   nVertSiz = nRows = rRect.bottom/nHt;
  480.   ShowScrollBar (hWnd, SB_VERT, (nRows<nCurMsg));
  481.   nLine = nVertPos;
  482.   if (lpMsg!=NULL)
  483.   {
  484.     for (nI=nPos=0; (nI<nRows) && (nLine<nCurMsg); nI++, nLine++, nPos+=nHt) 
  485.     {
  486.       if (lpMsg[nLine]!=NULL) 
  487.       {
  488.         TextOut (hDC, 20-nHorzPos, nPos, lpMsg[nLine], lstrlen (lpMsg[nLine]));
  489.       }
  490.     }
  491.   }
  492.   EndPaint(hWnd, &ps);
  493. } /* DoPaint */
  494.  
  495.  
  496.